//
//  CPLanguage.swift
//  BabaLink
//
//  Created by wrs on 2018/8/10.
//  Copyright © 2018年 wrs. All rights reserved.
//

import Foundation
fileprivate let CPLANGUAGE_KEY = "CPLANGUAGE_KEY"
fileprivate let LOCALIZABLE = "Localizable"
public let CPLANGUAGE_CHANGE_NOTIFICATION = NSNotification.Name("CPLanguageChangeNotification")

public enum CPLanguageType: String {
    case ZhHans = "zh-Hans"   // 简体中文
    case En = "en" // 英文
}

public func initCurrentLanguage() -> Void {
    // 如果首次UserDefaults没有保存用户语言，则将系统默认语言保存到UserDefaults
    if UserDefaults.standard.value(forKey: CPLANGUAGE_KEY) == nil {
        var languageType: CPLanguageType = .ZhHans
        // 获取系统默认的语言
        if let languages = UserDefaults.standard.object(forKey: "AppleLanguages")as? [String] {
            if languages.count > 0 {
                if let sysCurrent = languages.first {
                    if sysCurrent.contains(CPLanguageType.ZhHans.rawValue) {
                        languageType = .ZhHans
                    } else if sysCurrent.contains(CPLanguageType.En.rawValue) {
                        languageType = .En
                    }
                }
            }
        }
        // 保存到UserDefaults
        setUserlanguageType(languageType: languageType)
    }
}

public func getCurrentAppName() -> String {
    if let name = getLanguageBundle(languageType: getCurrentLanguage())?.localizedString(forKey: "CFBundleDisplayName", value: "", table: "InfoPlist") {
        return name
    } else {
        return ""
    }
}

public func setUserlanguageType(languageType: CPLanguageType) -> Void {
    UserDefaults.standard.set(languageType.rawValue, forKey: CPLANGUAGE_KEY)
    UserDefaults.standard.synchronize()
    
    NotificationCenter.default.post(name: CPLANGUAGE_CHANGE_NOTIFICATION, object: nil)
}

public func getCurrentLanguage() -> CPLanguageType {
    var languageType: CPLanguageType = .ZhHans
    if let languageStr = UserDefaults.standard.value(forKey: CPLANGUAGE_KEY) as? String { // 已经保存语言
        if let languageType = CPLanguageType(rawValue: languageStr) {
            return languageType
        }
    } else { // 没有保存语言，说明没有调用初始化方法
        if let languages = UserDefaults.standard.object(forKey: "AppleLanguages")as? [String] {
            if languages.count > 0 {
                let sysCurrent = languages.first!
                if sysCurrent.contains(CPLanguageType.ZhHans.rawValue) {
                    languageType = .ZhHans
                } else if sysCurrent.contains(CPLanguageType.En.rawValue) {
                    languageType = .En
                } else {
                    languageType = .En
                }
            }
        }
        setUserlanguageType(languageType: languageType)
    }
    return languageType
}

public func getLocalizedString(key: String, value: String?, table: String?, languageType: CPLanguageType) -> String {
    if let bundle = getLanguageBundle(languageType: languageType) {
        return bundle.localizedString(forKey: key, value: value, table: table)
    } else {
        return ""
    }
}

fileprivate func getLanguageBundle(languageType: CPLanguageType) -> Bundle? {
    let language = languageType.rawValue
    if let path = Bundle.main.path(forResource: language, ofType: "lproj") {
        let bundle = Bundle(path: path)
        return bundle
    } else {
        return nil
    }
}

public extension String {
    func localizedString(table: String?) -> String {
        var aa = LOCALIZABLE
        if let str = table {
            aa = str
        }
        return getLocalizedString(key: self, value: nil, table: aa, languageType: getCurrentLanguage())
    }
}
